<?php
/*
 * @Author: your name
 * @Date: 2020-11-10 13:44:09
 * @LastEditTime: 2020-12-28 10:33:11
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /special/application/adminapiv3/service/platform_manage/log/ServerUpdateLogService.php
 */

namespace app\adminapiv3\service\platform_manage\log;


use app\adminapiv3\service\BaseService;
use app\common\definition\HostDefinition;
use think\Db;
use app\common\definition\platform\UpdateLogDefinition;

class ServerUpdateLogService extends BaseService
{

    /**
     * @name 总览
     * @param $params
     * @return array
     * @author wx
     * @date 2020/5/11 10:26
     */
    public function hostUpdateList($params)
    {
        //获取当前资产组id
        $nowHostGroupId = $this->getLoginHostGroupId();
        $selectHostGroupArr = ['-1', $nowHostGroupId];

        $conditions[] = [
            'os',
            '>',
            HostDefinition::UNKNOW_OPERATION_SYSTEM
        ];
        if(isset($params['ip']) && !empty($params['ip']))
        {
            $conditions[] = [
                'ip',
                'like',
                '%' . $params['ip'] . '%',
            ];
        }
        //资产名
        if(isset($params['name']) && !empty($params['name']))
        {
            $conditions[] = [
                'name',
                'like',
                '%' . $params['name'] . '%',
            ];
        }
        //判定是否传参数客户端更新状态
        if(isset($params['client_version']) && !empty($params['client_version'])){
            $conditions[] = [
                'client_version',
                'like',
                '%' . $params['client_version'] . '%'
            ];
        }
        //判定是否上传客户端更新状态
        if(isset($params['client_update_status']) && is_numeric($params['client_update_status'])){
            $conditions[] = [
                'client_update_status',
                '=',
                $params['client_update_status']
            ];
        }
        //判定是否为 超管组 如果是超管组 看所有
        if(!$this->isRootRoleGroup())
        {
            $conditions[] = [
                'grouo_id',
                '=',
                $this->getLoginHostGroupId()
            ];
        }
        $data = Db::name('host')
            ->field('id,os,ip,name,client_version,client_update_status')
            ->where($conditions)
            ->whereIn('group_id', $selectHostGroupArr)
            ->order('id', 'DESC')
            ->paginate()->toArray();

        return $data;
    }

    /**
     * @name 详情
     * @param $params
     * @return array
     * @author wx
     * @date 2020/5/8 10:20
     */
    public function details($params)
    {
        $conditions[] = [
            'host_id',
            '=',
            $params['host_id']
        ];

        //条件筛选
        if(isset($params['old_version']) && !empty($params['old_version']))
        {
            $conditions[] = [
                'old_version',
                'like',
                '%' . $params['old_version'] . '%'
            ];
        }

        if(isset($params['new_version']) && !empty($params['new_version']))
        {
            $conditions[] = [
                'new_version',
                'like',
                '%' . $params['new_version'] . '%'
            ];
        }
        
        if(isset($params['ip']) && !empty($params['ip']))
        {
            $conditions[] = [
                'ip',
                'like',
                '%' . $params['ip'] . '%'
            ];
        }

        if(isset($params['status']) && !empty($params['status']))
        {
            $conditions[] = [
                'status',
                '=',
                $params['status']
            ];
        }

        if(isset($params['desc']) && !empty($params['desc']))
        {
            $conditions[] = [
                'desc',
                'like',
                '%' . $params['desc'] . '%'
            ];
        }

        if(isset($params['ctime']) && !empty($params['ctime']))
        {
            $conditions[] = [
                'ctime',
                'between',
                [
                    strtotime($params['ctime']),
                    strtotime($params['ctime'].' 23:59:59')
                ]
            ];
        }
        $data = Db::name('server_update_log')->where($conditions)->order('id' , 'DESC')->paginate()->toArray();

        $data['data'] = array_map(function($log){
            $log['ctime'] = date('Y-m-d H:i:s', $log['ctime']);
            $this->addShowName($log);
            return $log;
        }, $data['data']);

        return $data;
    }

    /**
     * @name: 添加展示名
     * @Param: 
     * @Description: 
     * @Author: foo
     * @Date: 2020-12-17 15:52:29
     */
    private function addShowName (&$log)
    {
        switch ($log['type'])
        {
            case UpdateLogDefinition::CLIENT_TYPE :
                $log['type_name'] = UpdateLogDefinition::CLIENT_TYPE_DESCRIPTION;
            break;
            case UpdateLogDefinition::SERVER_TYPE :
                $log['type_name'] = UpdateLogDefinition::SERVER_TYPE_DESCRIPTION;
            break;
            case UpdateLogDefinition::MIDDLEWARE_TYPE :
                $log['type_name'] = UpdateLogDefinition::MIDDLEWARE_TYPE_DESCRIPTION;
            break;
            default :
                $log['type_name'] = UpdateLogDefinition::UNKNOW_TYPE_DESCRIPTION;
        }

        switch ($log['status'])
        {
            case UpdateLogDefinition::DOWNLOADING_STATUS :
                $log['status_name'] = UpdateLogDefinition::DOWNLOADING_DESCRIPTION;
            break;
            case UpdateLogDefinition::DOWNLOAD_SUCCESS_STATUS :
                $log['status_name'] = UpdateLogDefinition::DOWNLOAD_SUCCESS_DESCRIPTION;
            break;
            case UpdateLogDefinition::DOWNLOAD_FAIL_STATUS :
                $log['status_name'] = UpdateLogDefinition::DOWNLOAD_FAIL_STATUS_DESCRIPTION;
            break;
            case UpdateLogDefinition::UPADRE_SUCCESS_STATUS :
                $log['status_name'] = UpdateLogDefinition::UPADRE_SUCCESS_STATUS_DESCRIPTION;
            break;
            case UpdateLogDefinition::UPADRE_FAIL_STATUS :
                $log['status_name'] = UpdateLogDefinition::UPADRE_FAIL_STATUS_DESCRIPTION;
            break;
            case UpdateLogDefinition::UPADRE_ADDRESS_SUCCESS_STATUS :
                $log['status_name'] = UpdateLogDefinition::UPADRE_ADDRESS_SUCCESS_STATUS_DESCRIPTION;
            break;
            default :
                $log['status_name'] = UpdateLogDefinition::UNKNOW_DESCRIPTION;
        }
    }
}